package org.heng.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.heng.commons.result.Result;
import org.heng.dto.req.BuyTicketReqDTO;
import org.heng.dto.req.CreateTicketReqDTO;
import org.heng.dto.req.TicketReqDTO;
import org.heng.dto.req.UpdateTicketDTO;
import org.heng.dto.resp.TicketRespDTO;
import org.heng.service.TicketService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@CrossOrigin("*")
@RestController
@RequiredArgsConstructor
public class SobController {
    private final TicketService ticketService;

    @PostMapping("/create")
    public Result<TicketRespDTO> createTicket(@RequestBody CreateTicketReqDTO requestParams) {
        System.out.println(requestParams);;
        return new Result<TicketRespDTO>().setMsg("创建成功").setData(ticketService.createTicket(requestParams));
    }

    @PostMapping("/sobing")
    public Result<Void> purchaseTicket(@RequestBody BuyTicketReqDTO requestParams) {
        ticketService.purchaseTicket(requestParams);
        return new Result<Void>().setMsg("购票成功");
    }

    @PostMapping("/query")
    public Result<TicketRespDTO> getRemainsTicket(@RequestBody TicketReqDTO requestParams) {
        return new Result<TicketRespDTO>().setData(ticketService.queryTicketsById(requestParams.getId()));
    }

    @PostMapping("/querybyname")
    public Result<TicketRespDTO> queryTicketByName(@RequestBody TicketReqDTO requestParams) {
        return new Result<TicketRespDTO>().setData(ticketService.queryTicketsByName(requestParams.getName()));
    }

    @PostMapping("/update")
    public Result<TicketRespDTO> updateStock(@RequestBody TicketReqDTO requestParams) {
        ticketService.updateStock(requestParams);
        return new Result<TicketRespDTO>().setMsg("新增成功");
    }
    @PostMapping("/updateprice")
    public Result<TicketRespDTO> updatePrice(@RequestBody UpdateTicketDTO requestParams) {
        ticketService.updatePrice(requestParams);
        return new Result<TicketRespDTO>().setMsg("修改成功");
    }
}
